package bdd.daos.impl;

import bdd.DBLink;
import java.util.List;

import bdd.beans.*;
import bdd.daos.*;
import bdd.daos.impl.DAOTypeImpl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class DAOJnttypeecImpl implements DAOJnttypeec {
	private DBLink dblink;

	public DAOJnttypeecImpl() {
		super();
		this.dblink = new DBLink();
	}

	@Override
	public Jnttypeec save(Jnttypeec o) {
		try {
			this.dblink.connexion();
			String s = "INSERT INTO JNTTYPEEC (NUMTYPE, NUMEC, NBHEURESMAQUETTE)" +
				" VALUES (" + o.getType().getNumtype() +
                                ", " + o.getEc().getNumec() +
				", " + o.getNbheuresmaquette() + ")";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return o;
	}

	@Override
	public Jnttypeec find(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Jnttypeec> findAll() {
		List<Jnttypeec> listeRetour = new ArrayList();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM JNTTYPEEC"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Jnttypeec retour = new Jnttypeec();
				retour.setType(new DAOTypeImpl().find(rs.getInt("NUMTYPE")));
				retour.setEc(new DAOEcImpl().find(rs.getInt("NUMEC")));
				retour.setNbheuresmaquette(rs.getInt("NBHEURESMAQUETTE"));
				listeRetour.add(retour);
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally {
			this.dblink.deconnexion();
		}
		return listeRetour;
	}

	@Override
	public void update(Jnttypeec o) {
		try {
			this.dblink.connexion();
			String s = "UPDATE JNTTYPEEC SET NBHEURESMAQUETTE=" + o.getNbheuresmaquette() + " WHERE NUMTYPE=" + o.getType().getNumtype() + " AND NUMEC="+ o.getEc().getNumec();

			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally {
			this.dblink.deconnexion();
		}
	}

	@Override
	public void delete(Jnttypeec o) {
		try {
			this.dblink.connexion();
			String s = "DELETE FROM JNTTYPEEC WHERE NUMTYPE=" + o.getType().getNumtype() + " AND NUMEC =" + o.getEc().getNumec();

			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	@Override
	public List<Jnttypeec> findByEc(Integer numEc) {
		List<Jnttypeec> liste = new ArrayList<Jnttypeec>();
		dblink.connexion();

		String query = "select * from jnttypeec where numec = "+numEc;

		try {
			PreparedStatement pst = dblink.getConnection().prepareStatement(query);
			ResultSet rs = pst.executeQuery();

			while(rs.next()) {
				Jnttypeec c = new Jnttypeec();

				c.setType(new DAOTypeImpl().find(rs.getInt("NUMEXAMEN")));
				c.setEc(new DAOEcImpl().find(rs.getInt("NUMEC")));
				c.setNbheuresmaquette(rs.getInt("NBHEURESMAQUETTE"));
				liste.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			dblink.deconnexion();
		}
		return liste;
	}

        public List<Jnttypeec> findByType(Integer numType) {
            List<Jnttypeec> liste = new ArrayList<Jnttypeec>();
            dblink.connexion();

            String query = "select * from jnttypeec where numtype = "+numType;

            try {
                    PreparedStatement pst = dblink.getConnection().prepareStatement(query);
                    ResultSet rs = pst.executeQuery();

                    while(rs.next()) {
                            Jnttypeec c = new Jnttypeec();

                            c.setType(new DAOTypeImpl().find(rs.getInt("NUMEXAMEN")));
                            c.setEc(new DAOEcImpl().find(rs.getInt("NUMEC")));
                            c.setNbheuresmaquette(rs.getInt("NBHEURESMAQUETTE"));
                            liste.add(c);
                    }
            } catch (SQLException e) {
                    e.printStackTrace();
            }finally {
                    dblink.deconnexion();
            }
            return liste;
        }
}
